package com.aura.springbook.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;


@Getter
@AllArgsConstructor
public enum BorrowStatusEnum {
    /**
     * 未归还
     * <p>
     * 图书仍在借阅中，未归还到图书馆
     */
    NOT_RETURNED(0, "未归还"),

    /**
     * 已归还
     * <p>
     * 图书已归还到图书馆
     */
    RETURNED(1, "已归还");

    private final int code;
    private final String description;

    /**
     * 根据code获取枚举
     * <p>
     * 通过状态码查找对应的枚举值
     *
     * @param code 状态码
     * @return 对应的枚举值
     * @throws IllegalArgumentException 当code无效时抛出异常
     */
    public static BorrowStatusEnum fromCode(int code) {
        for (BorrowStatusEnum status : BorrowStatusEnum.values()) {
            if (status.getCode() == code) {
                return status;
            }
        }
        throw new IllegalArgumentException("无效的借阅状态代码: " + code);
    }
}